package net.jxiny.oschina;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.ms_square.etsyblur.BlurringView;

import net.jxiny.oschina.tweet.TweetActivity.SendAnswerActivity;
import net.jxiny.oschina.tweet.TweetActivity.SendEssayActivity;
import net.jxiny.oschina.tweet.TweetActivity.SendTweetActivity;


public class MoreWindow extends PopupWindow implements View.OnClickListener {

        private Activity mContext;
        private RelativeLayout layout;
        private ImageView close;
        private View bgView;
        private BlurringView blurringView;
        private int mWidth;
        private int mHeight;
        private int statusBarHeight;
        private Handler mHandler = new Handler();

        public MoreWindow(Activity context) {
            mContext = context;
        }

        /**
         * 初始化
         *
         * @param view 要显示的模糊背景View,一般选择跟布局layout
         */
        public void init(View view) {
            Rect frame = new Rect();
            mContext.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            statusBarHeight = frame.top;
            DisplayMetrics metrics = new DisplayMetrics();
            mContext.getWindowManager().getDefaultDisplay()
                    .getMetrics(metrics);
            mWidth = metrics.widthPixels;
            mHeight = metrics.heightPixels;

            setWidth(mWidth);
            setHeight(mHeight);

            layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.activity_more_window, null);

            setContentView(layout);

            close = (ImageView) layout.findViewById(R.id.iv_close);
            close.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (isShowing()) {
                        closeAnimation();
                    }
                }

            });

            blurringView = (BlurringView) layout.findViewById(R.id.blurring_view);
            blurringView.blurredView(view);//模糊背景

            bgView = layout.findViewById(R.id.rel);
            setOutsideTouchable(true);
            setFocusable(true);
            setClippingEnabled(false);//使popupwindow全屏显示
        }

        public int getNavigationBarHeight(Activity activity) {
            Resources resources = activity.getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            //获取NavigationBar的高度
            int height = resources.getDimensionPixelSize(resourceId);
            return height;
        }

        /**
         * 显示window动画
         *
         * @param anchor
         */
        public void showMoreWindow(View anchor) {

            showAtLocation(anchor, Gravity.TOP | Gravity.START, 0, 0);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        //圆形扩展的动画
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                            int x = mWidth / 2;
                            int y = (int) (mHeight - fromDpToPx(25));
                            Animator animator = ViewAnimationUtils.createCircularReveal(bgView, x,
                                    y, 0, bgView.getHeight());
                            animator.addListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationStart(Animator animation) {
//                                bgView.setVisibility(View.VISIBLE);
                                }

                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    //							layout.setVisibility(View.VISIBLE);
                                }
                            });
                            animator.setDuration(300);
                            animator.start();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });


            showAnimation(layout);

        }

        private void showAnimation(ViewGroup layout) {
            try {
                LinearLayout linearLayout = layout.findViewById(R.id.lin);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        //＋ 旋转动画


                        close.animate().rotation(45).setDuration(400);

                    }
                });
                //菜单项弹出动画
                for (int i = 0; i < linearLayout.getChildCount(); i++) {
                    final View child = linearLayout.getChildAt(i);
                    child.setOnClickListener(this);
                    child.setVisibility(View.INVISIBLE);
                    mHandler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            child.setVisibility(View.VISIBLE);
                            ValueAnimator fadeAnim = ObjectAnimator.ofFloat(child, "translationY", 600, 0);
                            fadeAnim.setDuration(200);
                            KickBackAnimator kickAnimator = new KickBackAnimator();
                            kickAnimator.setDuration(150);
                            fadeAnim.setEvaluator(kickAnimator);
                            fadeAnim.start();
                        }
                    }, i * 50 + 100);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        /**
         * 关闭window动画
         */
        private void closeAnimation() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    close.animate().rotation(-45).setDuration(400);
                }
            });

            try {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {

                    int x = mWidth / 2;
                    int y = (int) (mHeight - fromDpToPx(25));
                    Animator animator = ViewAnimationUtils.createCircularReveal(bgView, x,
                            y, bgView.getHeight(), 0);
                    animator.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationStart(Animator animation) {
                            //							layout.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
//                        bgView.setVisibility(View.GONE);
                            dismiss();
                        }
                    });
                    animator.setDuration(300);
                    animator.start();
                }
            } catch (Exception e) {
            }
        }

        /**
         * 点击事件处理
         *
         * @param v
         */
        @Override
        public void onClick(View v) {

            if (isShowing()) {
                closeAnimation();
            }

            switch (v.getId()) {
                case R.id.tv_sbs:
                    Intent intent = new Intent(mContext, SendTweetActivity.class);
                    mContext.startActivity(intent);
                    break;
                case R.id.tv_search:
                    Intent intent1 = new Intent(mContext, SendAnswerActivity.class);
                    mContext.startActivity(intent1);
                    break;
                case R.id.tv_task:
                    Intent intent2 = new Intent(mContext, SendEssayActivity.class);
                    mContext.startActivity(intent2);
                    break;
            }

        }

        float fromDpToPx(float dp) {
            return dp * Resources.getSystem().getDisplayMetrics().density;
        }
    }